//
//  BADLinkViewController.m
//  BAD
//
//  Created by Can on 2018/5/15.
//  Copyright © 2018年 hwd. All rights reserved.
//

#import "BADLinkViewController.h"

static NSOperationQueue *queue;

@interface BADLinkViewController ()

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation BADLinkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    
    queue=[[NSOperationQueue alloc]init];
    
    NSInvocationOperation *op=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downLoadWeb) object:nil];
    
    [queue addOperation:op];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
}

- (void)creatView
{
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH)];
    [_webView setBackgroundColor:[UIColor clearColor]];
    [_webView setOpaque:NO];
    _webView.scalesPageToFit = YES;
    [self.view addSubview:_webView];
    
    UIImageView *backImageView = [UIViewControls creatImageView:CGRectMake(SCREENW - 25*screenW_6 - 35*screenW_6, 105/2*screenW_6, 35*screenW_6, 35*screenW_6) imageName:@"close2"];
    [self.view addSubview:backImageView];
    backImageView.userInteractionEnabled = YES;
    //添加点击手势（即点击图片后退出全屏）
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeView)];
    [backImageView addGestureRecognizer:tapGesture];
}

-(void)downLoadWeb
{
    
    NSURL *url=[NSURL URLWithString:_strUrl];
    
    NSError *error;
    
    NSString *strData=[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
    
    NSData *data=[strData dataUsingEncoding:NSUTF8StringEncoding];
    
    if (data !=nil) {
        
        [self performSelectorOnMainThread:@selector(downLoad_completed:) withObject:data waitUntilDone:NO];
        
    }
    else
    {
        NSLog(@"error when download:%@",error);
    }
}

-(void)downLoad_completed:(NSData *)data
{
    NSURL *url=[NSURL URLWithString:_strUrl];
    NSString *nameType=[self mimeType:url];
    [_webView loadData:data MIMEType:nameType textEncodingName:@"UTF-8" baseURL:url];
}

#pragma mark 获取指定URL的MIMEType类型
- (NSString *)mimeType:(NSURL *)url
{
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    //使用同步方法后去MIMEType
    NSURLResponse *response = nil;
    
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    
    return response.MIMEType;
}

-(void)closeView{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
